﻿using System;
using System.Linq;
using System.Web.UI;
using BookingEngine.Data;

namespace BookingEngine.Web.cn
{
    public partial class ReservationConfirm : Page
    {
        #region 字段

        private string _hotelCode;
        private string _roomCode;
        private string _rateCode;
        private DateTime _checkInDate;
        private DateTime _checkOutDate;
        private int _roomsNumber;
        private int _adultsNumber;
        private int _childrenNumber;

        private string _confnum;
        private decimal _totalPrice;

        private string _guestName;
        private string _guestMobile;
        private string _guestPhone;
        private string _guestEmail;

        #endregion

        #region 初始化参数

        private void InitializeParameters()
        {
            var hotelCode = Request["HotelCode"];
            var roomCode = Request["RoomCode"];
            var rateCode = Request["RateCode"];
            var checkInDate = Request["CheckInDate"];
            var checkOutDate = Request["CheckOutDate"];
            var roomsNumber = Request["RoomsNumber"];
            var adultsNumber = Request["AdultsNumber"];
            var childrenNumber = Request["ChildrenNumber"];

            var confnum = Request["Confnum"];
            var totalPrice = Request["TotalPrice"];

            var guestName = Request["GuestName"];
            var guestMobile = Request["Mobile"];
            var guestPhone = Request["Phone"];
            var guestEmail = Request["Email"];

            _hotelCode = string.IsNullOrEmpty(hotelCode) ? "0" : hotelCode.Trim();
            _roomCode = string.IsNullOrEmpty(roomCode) ? "FQ" : roomCode.Trim();
            _rateCode = string.IsNullOrEmpty(rateCode) ? "BAR" : rateCode.Trim();
            _checkInDate = string.IsNullOrEmpty(checkInDate) ? DateTime.Now.Date.AddDays(1) : DateTime.Parse(checkInDate);
            _checkOutDate = string.IsNullOrEmpty(checkOutDate) ? DateTime.Now.Date.AddDays(2) : DateTime.Parse(checkOutDate);
            _roomsNumber = string.IsNullOrEmpty(roomsNumber) ? 1 : int.Parse(roomsNumber);
            _adultsNumber = string.IsNullOrEmpty(adultsNumber) ? 1 : int.Parse(adultsNumber);
            _childrenNumber = string.IsNullOrEmpty(childrenNumber) ? 0 : int.Parse(childrenNumber);

            _confnum = string.IsNullOrEmpty(confnum) ? "(无)" : confnum.Trim();
            _totalPrice = string.IsNullOrEmpty(totalPrice) ? 0 : decimal.Parse(totalPrice);

            _guestName = string.IsNullOrEmpty(guestName) ? "(无)" : guestName.Trim();
            _guestMobile = string.IsNullOrEmpty(guestMobile) ? "(无)" : guestMobile.Trim();
            _guestPhone = string.IsNullOrEmpty(guestPhone) ? "(无)" : guestPhone.Trim();
            _guestEmail = string.IsNullOrEmpty(guestEmail) ? "(无)" : guestEmail.Trim();
        }

        #endregion

        #region 绑定数据

        private void BindData()
        {
            confnumContent.Text = _confnum;
            bookDateContent.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");

            checkInDateContent.Text = _checkInDate.ToString("yyyy年MM月dd日");
            checkOutDateContent.Text = _checkOutDate.ToString("yyyy年MM月dd日");
            roomsNumberContent.Text = string.Format("{0}间", _roomsNumber);
            adultsNumberContent.Text = string.Format("{0}人", _adultsNumber);
            childrenNumberContent.Text = string.Format("{0}人", _childrenNumber);

            totalPriceContent.Text = _totalPrice.ToString("#,##0.00");

            guestNameContent.Text = _guestName;
            guestMobileContent.Text = _guestMobile;
            guestPhoneContent.Text = _guestPhone;
            guestEmailContent.Text = _guestEmail;

            var hotel = HotelSearchService.GetHotel("zh", _checkInDate, _checkOutDate, _hotelCode);
            if (hotel != null)
            {
                hotelTitleContent.Text = hotel.hotelName;
                hotelNameContent.Text = hotel.hotelName;
                hotelPhoneContent.Text = hotel.phone;
                hotelAddressContent.Text = hotel.hotelAddr;

                var currentRoom = hotel.rooms != null ? hotel.rooms.FirstOrDefault(room => room.roomCode == _roomCode) : null;
                if (currentRoom != null)
                {
                    roomNameContent.Text = currentRoom.roomName;
                    roomDescriptionContent.Text = string.Format("● 床型: {0} ● 面积: {1} ● 楼层: {2} ● 加床: {3} ● 宽带: {4} ● 无烟房: {5} ● 窗户: {6}", currentRoom.bedtypeName, currentRoom.area, currentRoom.floor, currentRoom.isaddbad, currentRoom.isNet, currentRoom.isnosmoke, currentRoom.iswindow);

                    var currentRatePlan = currentRoom.ratePlans != null && currentRoom.ratePlans.Length > 0 ? currentRoom.ratePlans.FirstOrDefault(ratePlan => ratePlan.ratecode == _rateCode) : null;
                    if (currentRatePlan != null)
                    {
                        ratePlanContent.Text = currentRatePlan.ratePlanName;
                        guaranteeRuleContent.Text = currentRatePlan.garanteeRule != null ? currentRatePlan.garanteeRule.ruleDesc : "(无)";
                        cancellationRuleContent.Text = currentRatePlan.cancelRule != null ? currentRatePlan.cancelRule.ruleDesc : "(无)";

                        if (currentRatePlan.rates != null)
                        {
                            ratesContent.Text = _adultsNumber > 1 ? currentRatePlan.rates.Aggregate("", (s, rate) => string.Format("{0}；{1} : 价格:{2}", s, rate.availDate, rate.doubleRate.ToString("#,##0.00"))).Trim('；') : currentRatePlan.rates.Aggregate("", (s, rate) => string.Format("{0}；{1} : 价格:{2}", s, rate.availDate, rate.singleRate.ToString("#,##0.00"))).Trim('；');
                        }
                    }
                }
            }
        }

        #endregion

        #region 事件处理

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitializeParameters();
                BindData();
            }
        }

        #endregion
    }
}